/**
 * @author: jw
 */

/**
 * @description 保存文档
 */
export const save = {
  name: "保存文档",
  url: "doc/save",
  method: "post",
  param: "",
  paramSet(param) {
    return {
      data: param,
      url: this.url,
      method: this.method
    };
  },
  result: {
    code: 1,
    data: {},
    message: "ok"
  },
  // 模拟数据
  resultMock(param) {
    param = Object.assign({}, param);
    return new Promise((resolve) => {
      window.db.put("doc", param, (ev = {}) => {
        console.log("保存文档", param, ev);
        let res = ev.target || {};
        let error = res.error || ev.error;
        res.result && (param.id = res.result);
        let result = Object.assign(this.result, {
          code: error ? 0 : 1,
          message: error && error.name == "ConstraintError" ? "文档名称重复" : error,
          data: param
        });
        resolve(result);
      });
    });
  }
};

/**
 * @description 删除文档
 */
export const remove = {
  name: "删除文档",
  url: "doc/remove",
  method: "get",
  paramDefault: "",
  paramSet(param) {
    return {
      param: param,
      url: this.url,
      method: this.method
    };
  },
  result: {
    code: 1,
    data: {},
    message: "ok"
  },
  // 模拟数据
  resultMock(id) {
    return new Promise((resolve) => {
      window.db.del("doc", id, (ev) => {
        var appId = ev.target.result;
        console.log("删除文档", appId);
        let result = Object.assign(this.result, {
          data: appId
        });
        resolve(result);
      });
    });
  }
};

/**
 * @description 根据id查询文档
 */
export const get = {
  name: "查询文档",
  url: "doc/get",
  method: "get",
  paramDefault: "",
  paramSet(param) {
    return {
      param: param,
      url: this.url,
      method: this.method
    };
  },
  result: {
    code: 1,
    data: {},
    message: "ok"
  },
  // 模拟数据
  resultMock(id) {
    return new Promise((resolve) => {
      window.db.get("doc", id, (ev) => {
        var app = ev.target.result;
        console.log("查询文档", app);
        let result = Object.assign(this.result, {
          data: app
        });
        resolve(result);
      });
    });
  }
};

/**
 * @description 查询文档列表
 */
export const getList = {
  name: "查询文档列表",
  url: "doc/getList",
  method: "post",
  paramDefault: "",
  paramSet(param) {
    return {
      params: param,
      url: this.url,
      method: this.method
    };
  },
  result: {
    code: 1,
    data: [],
    message: "ok"
  },
  // 模拟数据
  resultMock(param = {}) {
    return new Promise((resolve) => {
      let result = Object.assign({}, this.result);
      let list = [];
      let name = param.keyword || "";
      window.db.find("doc", (ev) => {
        var cursor = ev.target.result;
        // console.log("[getList]", cursor);
        if (cursor) {
          let value = cursor.value || {};
          // console.log("[list][value]", value, param);
          let hit = true;
          if (name && name.indexOf(param.name) < 0) {
            hit = false;
          }
          hit && list.push(value);
          cursor.continue();
        } else {
          result = Object.assign(result, {
            data: list
          });
          resolve(result);
          // console.log("[list]", list, param, result);
        }
      });
    });
  }
};
/**
 * @description 缓存文档(缓存当前编辑的文档)
 */
export const cache = {
  name: "缓存文档",
  url: "doc/cache",
  method: "get",
  paramDefault: "",
  paramSet(param) {
    param = param || this.param;
    return {
      params: param,
      url: this.url,
      method: this.method
    };
  },
  result: {
    code: 0,
    data: null,
    message: "缓存文档失败"
  },
  // 模拟数据
  resultMock(param) {
    return new Promise((resolve) => {
      if (!param) {
        resolve(this.result);
      }
      let cache = {
        id: 0,
        apiDoc: param
      };
      window.db.put("cache", cache, (ev) => {
        var res = ev.target.result;
        console.log("[getCache]", res);
        let result = Object.assign(this.result, {
          data: res
        });
        resolve(result);
      });
    });
  }
};
/**
 * @description 查询缓存文档(上次编辑的文档)
 */
export const getCache = {
  name: "查询缓存文档",
  url: "doc/getCache",
  method: "get",
  paramDefault: "",
  paramSet(param) {
    param = param || this.param;
    return {
      params: param,
      url: this.url,
      method: this.method
    };
  },
  result: {
    code: 0,
    data: null,
    message: "没有缓存文档"
  },
  // 模拟数据
  resultMock(param = {}) {
    return new Promise((resolve) => {
      window.db.get("cache", 0, (ev) => {
        var res = ev.target.result;
        console.log("[getCache]", res);
        let result = this.result;
        if (res && res.apiDoc) {
          result = {
            code: 1,
            data: res.apiDoc,
            message: "ok"
          };
        }
        resolve(result);
      });
    });
  }
};
